﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class SearchAdvanced : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = -1;
            if (!IsPostBack)
            {
                conn.Open();
                SqlCommand cmdSearch = new SqlCommand("SELECT cat_name FROM Category WHERE cat_status='publish' ORDER BY cat_name", conn);
                SqlDataReader dtrSearch = cmdSearch.ExecuteReader();
                DropDownList1.DataSource = dtrSearch;
                DropDownList1.DataTextField = "cat_name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("-------", String.Empty));
                DropDownList1.SelectedIndex = 0;
                dtrSearch.Close();

                if (Request.QueryString["price"] != "")
                {
                    String query = "SELECT * FROM Products p, Users u, Category c WHERE p.p_category=c.cat_id AND p.u_id=u.u_id AND p_status='Approve' AND u_status='Activate'";

                    if (Request.QueryString["p"] == "1")
                    {
                        query += " AND p_price <'100'";
                        txtTo.Text = "100";
                    }
                    else if (Request.QueryString["p"] == "2")
                    {
                        query += " AND p_price >='100' AND p_price <='200'";
                        txtFrom.Text = "100";
                        txtTo.Text = "200";
                    }
                    else if (Request.QueryString["p"] == "3")
                    {
                        query += " AND p_price >='200' AND p_price <='300'";
                        txtFrom.Text = "200";
                        txtTo.Text = "300";
                    }
                    else if (Request.QueryString["p"] == "4")
                    {
                        query += " AND p_price >='300' AND p_price <='500'";
                        txtFrom.Text = "300";
                        txtTo.Text = "400";
                    }
                    else if (Request.QueryString["p"] == "5")
                    {
                        query += " AND p_price >='500'";
                        txtFrom.Text = "500";
                    }

                    SqlCommand cmdProduct = new SqlCommand(query, conn);
                    int num = Convert.ToInt32(cmdProduct.ExecuteScalar());

                    if (num != 0)
                    {
                        SqlDataReader dtrProduct = cmdProduct.ExecuteReader();

                        ItemList.DataSource = dtrProduct;
                        ItemList.DataBind();
                        dtrProduct.Close();

                        MultiView1.ActiveViewIndex = 0;
                    }
                    else
                    {
                        MultiView1.ActiveViewIndex = 1;
                    }
                }

                conn.Close();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SqlCommand cmdProduct, cmdSearch;
            SqlDataReader dtrProduct;
            conn.Open();

            if (txtName.Text == "" && txtDesc.Text == "" && txtSeller.Text == "" && txtFrom.Text == "" && txtTo.Text == "" && DropDownList1.SelectedValue == "")
            {
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                String query = "SELECT * FROM Products p, Users u WHERE p.u_id=u.u_id";

                if (txtName.Text != "")
                    query += " AND p_name LIKE '%" + txtName.Text + "%'";

                if (txtDesc.Text != "")
                    query += " AND p_desc LIKE '%" + txtDesc.Text + "%'";

                if (txtSeller.Text != "")
                    query += " AND username ='" + txtSeller.Text + "'";

                if (txtFrom.Text != "")
                    query += " AND p_price >='" + txtFrom.Text + "'";

                if (txtTo.Text != "")
                    query += " AND p_price <='" + txtTo.Text + "'";

                if (DropDownList1.SelectedValue != "")
                {
                    cmdSearch = new SqlCommand("SELECT cat_id FROM Category WHERE cat_name='" + DropDownList1.SelectedValue + "'", conn);
                    String catid = cmdSearch.ExecuteScalar().ToString();
                    query += " AND p_category ='" + catid + "'";
                }

                cmdProduct = new SqlCommand(query, conn);
                int num = Convert.ToInt32(cmdProduct.ExecuteScalar());

                if (num != 0)
                {
                    dtrProduct = cmdProduct.ExecuteReader();

                    ItemList.DataSource = dtrProduct;
                    ItemList.DataBind();
                    dtrProduct.Close();

                    MultiView1.ActiveViewIndex = 0;
                }
                else
                {
                    MultiView1.ActiveViewIndex = 1;
                }

                conn.Close();
            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Redirect("SearchAdvanced.aspx");
        }
    }
}